package com.beermanager.activities;

import android.content.Context;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.beermanager.R;
import com.beermanager.domain.Beer;
import com.beermanager.facebook.FacebookConnection;
import com.doffman.dragarea.DragArea;
import com.doffman.dragarea.DragEvent;

public class BeerDraggable extends LinearLayout {
    private Context mContext;
    private DragArea mDragArea;

    private Beer mBeer;
    private TextView mBeerName;
    private com.doffman.dragarea.OnDragListener mDragListener;
    private ImageView mBeerImage;
    private Drawable mBeerIcon;

    private boolean mStartDarg = false;

    public BeerDraggable(Context context, Beer beer) {
        super(context);
        mContext = context;
        mBeer = beer;
        initDraggableBeer(beer);
    }

    public BeerDraggable(Context context, AttributeSet attrs, Beer beer) {
        super(context, attrs);
        mContext = context;
        mBeer = beer;
        initDraggableBeer(beer);
    }

    public BeerDraggable(Context context, AttributeSet attrs, int defStyle, Beer beer) {
        super(context, attrs, defStyle);
        mContext = context;
        mBeer = beer;
        initDraggableBeer(beer);
    }

    private void initDraggableBeer(Beer beer){
        mBeerImage = new ImageView(getContext());
        if(beer.getPicture() != null) mBeerImage.setImageBitmap(beer.getPicture());
        else {
            mBeerImage.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.ic_home));
        }
        LinearLayout.LayoutParams layoutParams;
        layoutParams = new LayoutParams((int)mContext.getResources().getDimension(R.dimen.beer_icon_width),
                (int)mContext.getResources().getDimension(R.dimen.beer_icon_height));
        mBeerImage.setLayoutParams(layoutParams);
        mBeerImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
        mBeerImage.setPadding(10, 10, 10, 10);

        mBeerIcon = new BitmapDrawable(mBeer.getPicture());

        mBeerName = new TextView(getContext());
        mBeerName.setText(beer.getName());
        mBeerName.setGravity(Gravity.CENTER);

        this.setOrientation(VERTICAL);
        this.setGravity(Gravity.CENTER);
        this.addView(mBeerImage);
        this.addView(mBeerName);
        this.mBeer = beer;

        initDragAreaListener();
    }

    private void initDragAreaListener(){
        mDragListener = new com.doffman.dragarea.OnDragListener()
        {
            @Override
            public void onDrag(View view, DragEvent dragEvent)
            {
                switch (dragEvent.getAction())
                {
                    case DragEvent.ACTION_DRAG_STARTED:
                        if(mStartDarg)BeerDraggable.this.setVisibility(INVISIBLE);
                        break;
                    case DragEvent.ACTION_DRAG_ENTERED:
                        break;
                    case DragEvent.ACTION_DRAG_EXITED:
                        break;
                    case DragEvent.ACTION_DROP:
                        break;
                    case DragEvent.ACTION_DRAG_ENDED:
                        BeerDraggable.this.setVisibility(VISIBLE);
                        FacebookConnection.setBeer(mBeer);
                        mStartDarg = false;
                        break;
                    default:
                        break;
                }
            }


        };

    }

    public void setDragArea(DragArea dragArea) {
        mDragArea = dragArea;
        dragArea.addDragListener(BeerDraggable.this, mDragListener);

        this.setOnTouchListener(new View.OnTouchListener()
        {
            @Override
            public boolean onTouch(View view, MotionEvent event)
            {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    Bundle data = new Bundle();
                    data.putCharSequence(mBeer.getName(), (String)getTag());
                    mStartDarg = true;
                    mDragArea.startDrag(data,
                            new DrawableDragShadowBuilder(
                                    BeerDraggable.this,
                                    mBeerIcon,
                                    new Point((int)event.getX() - getPaddingLeft(),
                                            (int)event.getY() - getPaddingTop())));

                    return true;
                } else {
                    return false;
                }
            }
        });

    }

    public void removeFromDragArea(){
        mDragArea.removeDragListener(mDragListener);
    }

}
